<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API测试页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .test-section {
            margin: 20px 0;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        button {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 15px;
            border-radius: 3px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .result {
            margin-top: 10px;
            padding: 10px;
            background-color: #f8f9fa;
            border-radius: 3px;
            white-space: pre-wrap;
        }
        .success {
            border-left: 4px solid #28a745;
        }
        .error {
            border-left: 4px solid #dc3545;
        }
    </style>
</head>
<body>
    <h1>小智医疗版 API 测试</h1>
    
    <div class="test-section">
        <h3>健康分析 API 测试</h3>
        <button onclick="testGenerateAnalysis()">测试生成健康分析</button>
        <button onclick="testGetUserAnalytics()">测试获取用户分析记录</button>
        <div id="health-result" class="result"></div>
    </div>

    <div class="test-section">
        <h3>通知 API 测试</h3>
        <button onclick="testNotificationAPI()">测试通知API</button>
        <div id="notification-result" class="result"></div>
    </div>

    <script>
        // 模拟请求拦截器逻辑
        async function makeRequest(url, options = {}) {
            try {
                const response = await fetch(url, {
                    headers: {
                        'Content-Type': 'application/json',
                        ...options.headers
                    },
                    ...options
                });
                
                const data = await response.json();
                
                // 模拟新的统一响应格式处理
                if (data.success === true) {
                    return data;
                } else {
                    throw new Error(data.message || '请求失败');
                }
            } catch (error) {
                throw error;
            }
        }

        async function testGenerateAnalysis() {
            const resultDiv = document.getElementById('health-result');
            resultDiv.textContent = '正在测试...';
            
            try {
                const result = await makeRequest('http://localhost:8080/api/health-analytics/generate', {
                    method: 'POST',
                    body: JSON.stringify({
                        userId: 1,
                        analysisType: 'COMPREHENSIVE',
                        startDate: '2024-01-01T00:00:00',
                        endDate: '2024-12-31T23:59:59'
                    })
                });
                
                resultDiv.className = 'result success';
                resultDiv.textContent = '✅ 测试成功！\n' + JSON.stringify(result, null, 2);
            } catch (error) {
                resultDiv.className = 'result error';
                resultDiv.textContent = '❌ 测试失败：' + error.message;
            }
        }

        async function testGetUserAnalytics() {
            const resultDiv = document.getElementById('health-result');
            resultDiv.textContent = '正在测试...';
            
            try {
                const result = await makeRequest('http://localhost:8080/api/health-analytics/user/1?pageNum=1&pageSize=10');
                
                resultDiv.className = 'result success';
                resultDiv.textContent = '✅ 测试成功！\n' + JSON.stringify(result, null, 2);
            } catch (error) {
                resultDiv.className = 'result error';
                resultDiv.textContent = '❌ 测试失败：' + error.message;
            }
        }

        async function testNotificationAPI() {
            const resultDiv = document.getElementById('notification-result');
            resultDiv.textContent = '正在测试...';
            
            try {
                const result = await makeRequest('http://localhost:8080/api/notifications/user/1?pageNum=1&pageSize=10');
                
                resultDiv.className = 'result success';
                resultDiv.textContent = '✅ 测试成功！\n' + JSON.stringify(result, null, 2);
            } catch (error) {
                resultDiv.className = 'result error';
                resultDiv.textContent = '❌ 测试失败：' + error.message;
            }
        }

        // 页面加载完成后显示说明
        window.onload = function() {
            console.log('API测试页面已加载');
            console.log('请确保后端服务器正在运行在 http://localhost:8080');
        };
    </script>
</body>
</html>